This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
QnAMakerRecognizer.js
126 lines (126 loc) · 5.27 KB
/
QnAMakerRecognizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var request = require("request");
var entities = require("html-entities");
var qnaMakerV2ServiceEndpoint = 'https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/';
var qnaMakerServiceEndpoint = null;
var endpointHostName = null;
var qnaApi = 'generateanswer';
var qnaTrainApi = 'train';
var htmlentities = new entities.AllHtmlEntities();
var QnAMakerRecognizer = (function () {
function QnAMakerRecognizer(options) {
this.options = options;
if (this.options.endpointHostName != null) {
var hostName = this.options.endpointHostName.toLowerCase();
if (hostName.indexOf('https://') > -1)
hostName = hostName.split('/')[2];
if (hostName.indexOf("qnamaker") > -1) {
hostName = hostName.split('/')[0];
}
hostName = hostName.replace("/", "");
this.kbUri = 'https://' + hostName + '/qnamaker/knowledgebases/' + this.options.knowledgeBaseId + '/' + qnaApi;
this.authHeader = 'Authorization';
var re = /endpointkey/gi;
if (this.options.authKey.search(re) > -1) {
this.authorizationKey = this.options.authKey.trim();
}
else {
this.authorizationKey = 'EndpointKey ' + this.options.authKey.trim();
}
}
else {
this.kbUri = qnaMakerV2ServiceEndpoint + this.options.knowledgeBaseId + '/' + qnaApi;
this.kbUriForTraining = qnaMakerV2ServiceEndpoint + this.options.knowledgeBaseId + '/' + qnaTrainApi;
this.authHeader = 'Ocp-Apim-Subscription-Key';
this.authorizationKey = this.options.authKey;
}
this.intentName = options.intentName || "qna";
if (typeof this.options.top !== 'number') {
this.top = 1;
}
else {
this.top = this.options.top;
}
}
QnAMakerRecognizer.prototype.recognize = function (context, cb) {
var result = { score: 0.0, answers: null, intent: null };
if (context && context.message && context.message.text) {
var utterance = context.message.text;
QnAMakerRecognizer.recognize(utterance, this.kbUri, this.authorizationKey, this.authHeader, this.top, this.intentName, function (error, result) {
if (!error) {
cb(null, result);
}
else {
cb(error, null);
}
});
}
};
QnAMakerRecognizer.recognize = function (utterance, kbUrl, authkey, authHeader, top, intentName, callback) {
var _a;
try {
request({
url: kbUrl,
method: 'POST',
headers: (_a = {},
_a[authHeader] = authkey,
_a),
json: {
question: utterance,
top: top
}
}, function (error, response, result) {
var result;
try {
if (!error) {
if (response.statusCode === 200) {
var answerEntities = [];
if (result.answers && result.answers.length > 0) {
result.answers.forEach(function (ans) {
ans.score /= 100;
ans.answer = htmlentities.decode(ans.answer);
if (ans.questions && ans.questions.length > 0) {
ans.questions = ans.questions.map(function (q) { return htmlentities.decode(q); });
}
var answerEntity = {
score: ans.score,
entity: ans.answer,
type: 'answer'
};
answerEntities.push(answerEntity);
});
result.score = result.answers[0].score;
result.entities = answerEntities;
result.intent = intentName;
}
}
else {
error = new Error("QnA request returned a " + response.statusCode + " code with body: " + result);
}
}
}
catch (e) {
error = e;
}
try {
if (!error) {
callback(null, result);
}
else {
var m = error.toString();
callback(error instanceof Error ? error : new Error(m));
}
}
catch (e) {
console.error(e.toString());
}
});
}
catch (e) {
callback(e instanceof Error ? e : new Error(e.toString()));
}
};
return QnAMakerRecognizer;
}());
exports.QnAMakerRecognizer = QnAMakerRecognizer;